package com.songyu;

import com.songyu.mapper.UserMapper;
import com.songyu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/*
* mybatis的mapper代理开发
* */
public class test_mapper_proxy {
    public static void main(String[] args) throws IOException {
        // 1. 加载mybatis的核心配置文件， 获取sqlSessionFactory对象
        String resource = "com/songyu/mapper/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 2. 获取SqlSession对象用来执行sql语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 3. 获取mapper接口代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.selectAll();
        System.out.println(users);
        // 4. 释放资源
        sqlSession.close();
    }
}
